/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-app-api
*
* @File name : AppRoadSosController.java
*
* @Author : Administrator
*
* @Date : 2017年11月29日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年11月29日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.marketing.app.api.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.AppResponse;
import org.marketing.common.constants.ExceptionMessage;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.marketing.app.api.constant.AppConstant;
import com.yonyou.marketing.app.api.model.TtSoshelp;
import com.yonyou.marketing.app.api.service.AppSosService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 * @author Administrator
 * @date 2017年11月29日
 */
@RestController
@RequestMapping(value = AppConstant.BASE_PATH + "/" + AppConstant.VERSION)
@Api("APP道路救援API")
public class AppRoadSosController {

	private Logger logger = Logger.getLogger(AppRoadSosController.class);

	@Autowired
	private AppSosService appSosService;

	@RequestMapping(value = "/roadsideAssistance", method = RequestMethod.POST)
	@ApiOperation(value = "提交道路救援", notes = "提交道路救援")
	public AppResponse<Map<String, Object>> roadsideAssistance(
			@RequestParam(required = false, defaultValue = "") String userId,
			@RequestParam(required = false, defaultValue = "") String dealerCode,
			@RequestParam(required = false, defaultValue = "") String longitude,
			@RequestParam(required = false, defaultValue = "") String latitude,
			@RequestParam(required = false, defaultValue = "") String helpLocal) {
		logger.info("AppLoginController.roadsideAssistance==>start...");
		try {
			TtSoshelp ttSoshelp = new TtSoshelp();
			if (CheckUtil.NullOrEmpty(longitude)) {
				throw new ServiceBizException(ExceptionMessage.APP_LOCATE_INFO_ERROR);
			}
			BigDecimal lon = new BigDecimal(longitude);
			ttSoshelp.setLongitude(lon);
			if (CheckUtil.NullOrEmpty(latitude)) {
				throw new ServiceBizException(ExceptionMessage.APP_LOCATE_INFO_ERROR);
			}
			BigDecimal lat = new BigDecimal(latitude);
			ttSoshelp.setLatitude(lat);
			if (CheckUtil.NullOrEmpty(helpLocal)) {
				throw new ServiceBizException(ExceptionMessage.APP_LOCATE_INFO_ERROR);
			}
			ttSoshelp.setHelpLocal(helpLocal);
			ttSoshelp.setDealerCode(dealerCode);
			Integer resultCode = null;
			resultCode = appSosService.roadsideAssistance(ttSoshelp, userId);
			if (resultCode == 0) {
				return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, ExceptionMessage.APP_SEND_SOS_FAIL);
			}
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("提交状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppLoginController.roadsideAssistance==>end...");
		}
	}

	@RequestMapping(value = "/nearDealer", method = RequestMethod.POST)
	@ApiOperation(value = "道路救援可选销售店", notes = "道路救援可选销售店")
	public AppResponse<List<Map<String, Object>>> getDealer(
			@RequestParam(required = false, defaultValue = "") String userId,
			@RequestParam(required = false, defaultValue = "") String longitude,
			@RequestParam(required = false, defaultValue = "") String latitude) {
		logger.info("AppLoginController.getDealer==>start...");
		try {
			logger.info("AppLoginController.getDealer==>参数：userId:"+userId+"-longitude:"+longitude+"latitude:"+latitude);
			if (CheckUtil.NullOrEmpty(userId)) {
				throw new ServiceBizException(ExceptionMessage.USERID_IS_NULL);
			}
			TtSoshelp ttSoshelp = new TtSoshelp();
			if (CheckUtil.NullOrEmpty(longitude)) {
				throw new ServiceBizException(ExceptionMessage.APP_LOCATE_INFO_ERROR);
			}
			BigDecimal lon = new BigDecimal(longitude);
			ttSoshelp.setLongitude(lon);
			if (CheckUtil.NullOrEmpty(latitude)) {
				throw new ServiceBizException(ExceptionMessage.APP_LOCATE_INFO_ERROR);
			}
			BigDecimal lat = new BigDecimal(latitude);
			ttSoshelp.setLatitude(lat);
			List<Map<String, Object>> resultList = new ArrayList<>();
			resultList = appSosService.getDealer(ttSoshelp, userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, resultList, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppLoginController.getDealer==>end...");
		}
	}
}
